/*
Copyright (c) 2006 Fehmi Can SAGLAM             

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
*/

#ifndef _utilities_h__
#define _utilities_h__

#include <sys/time.h>
#include <string>

//----------------------------------------------------------------------------//
//                                                                            //
//----------------------------------------------------------------------------//
int strlen(char *str)
{
 	int len = 0;
	
	while(*str++)
	len++;
	
	return len; 
}

//----------------------------------------------------------------------------//
//                                                                            //
//----------------------------------------------------------------------------//
char *strrev(char * source)
{
	int i,j;
	char dest;
	int n=strlen(source);
	for(i=0,j=n-1;i<j;i++,j--)
	{
		dest = source[i];
		source[i]=source[j];
		source[j]= dest;
	}
	source[n]='\0';

	return source;
}

//----------------------------------------------------------------------------//
//                                                                            //
//----------------------------------------------------------------------------//
char* itoa(int value, char*  str, int radix)
{
	int  rem = 0;
	int  pos = 0;
	char ch  = '!' ;
	do
	{
		rem    = value % radix ;
		value /= radix;
		if ( 16 == radix )
		{
			if( rem >= 10 && rem <= 15 )
			{
				switch( rem )
				{
					case 10:
						ch = 'a' ;
						break;
					case 11:
						ch ='b' ;
						break;
					case 12:
						ch = 'c' ;
						break;
					case 13:
						ch ='d' ;
						break;
					case 14:
						ch = 'e' ;
						break;
					case 15:
						ch ='f' ;
						break;
				}
			}
		}
		if( '!' == ch )
		{
			str[pos++] = (char) ( rem + 0x30 );
		}
		else
		{
			str[pos++] = ch ;
		}
	}while( value != 0 );
	str[pos] = '\0' ;
	return strrev(str);
}

//----------------------------------------------------------------------------//
//                                                                            //
//----------------------------------------------------------------------------//
string GenerateUniqueFileName(string fileStart)
{
  struct timeval tv;
  gettimeofday(&tv, NULL);	// tv = current time
  char append[20];
  return fileStart + itoa(tv.tv_usec, append, 10) + itoa(tv.tv_sec, append, 16); 		 
}
#endif
